﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.DAO.基础;
using LibrarySolution.Model.基础;
using LibrarySolution.ComMethod;

namespace LibrarySolution.Frm
{
    public partial class FrmXjts : Form
    {
        #region 属性
        YhDao yhDao;
        JHBkDao jhbkDao;
        JSRecord jsEntity;
        YhxxEntity yhxxEntity;
        private static FrmXjts Instance;
        #endregion

        #region 构造器
        public FrmXjts()
        {
            InitializeComponent();
            if (GlobalVar.NoGly ==false && GlobalVar.SpGly==false)
            {
                TxtQueryByYhNo.Text = GlobalVar.YhxxEntity.yh_no;
                TxtQueryByYhNo.Enabled = false;
                BtnChange.Visible = false;
            }
        }
        #endregion

        #region 事件
        public static FrmXjts GetInstance()
        {
            if (Instance == null || Instance.IsDisposed)
            {
                Instance = new FrmXjts();
                Instance.MdiParent = GlobalVar.frmMain;
                GlobalVar.frmMain.panel1.Controls.Add(Instance);
                Instance.BringToFront();
            }
            return Instance;
        }

        private void BtnChange_Click(object sender, EventArgs e)
        {
            SearchPer();
        }

        private void BtnXjts_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource == null) return;
            String bkName = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            String jlCode = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
            String oldTime = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
            if (MessageBox.Show("您要续借的书是《" + bkName + "》?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                if (DateTime.Now > DateTime.Parse(oldTime))
                {
                    MessageBox.Show("续借失败,超过续借期限!");
                    return;
                }
                jsEntity = new JSRecord();
                jsEntity.js_ghrq = DataUtil.turnTime(oldTime, yhxxEntity.xjqx);
                jsEntity.js_code = jlCode;
                jsEntity.js_yhno = yhxxEntity.yh_no;
                jhbkDao.updateXjxx(jsEntity);
                MessageBox.Show("续借成功");
                dataGridView1.DataSource = jhbkDao.getMybook(jsEntity);
                dataGridView1.Columns["记录编码"].Visible = false;
                dataGridView1.Refresh();
            }
        }

        private void FrmXjts_Load(object sender, EventArgs e)
        {
            if (GlobalVar.NoGly == false && GlobalVar.SpGly == false) Instance.SearchPer();
        }
        #endregion

        #region 方法
        void SearchPer()
        {
            String yhNo;
            DataTable dt;
            yhDao = new YhDao();
            jhbkDao = new JHBkDao();
            jsEntity = new JSRecord();
            yhxxEntity = new YhxxEntity();
            yhNo = this.TxtQueryByYhNo.Text;
            jsEntity.js_yhno = yhNo;
            yhxxEntity = yhDao.getYhxxByNo(yhNo, "1");
            if (yhxxEntity.yh_no != null && yhxxEntity.yh_code != null)
            {
                dt = jhbkDao.getMybook(jsEntity);
                if (dt.Rows.Count == 0) {MessageBox.Show("您未借图书"); return;}
                dataGridView1.DataSource = dt;
                dataGridView1.Columns["记录编码"].Visible = false;
                dataGridView1.Refresh();
            }
            else
            {
                MessageBox.Show("用户不存在");
                dataGridView1.DataSource = null;
            }
            TxtQueryByYhNo.Text = "";
        }
        #endregion

    }
}
